﻿

Public Class BusinessFunction

#Region "Declaration"

    Public Const mscQuantity As String = "Quantity"
    Public Const mscUnitPrice As String = "UnitPrice"
    Public Const mscMainUnitPrice As String = "MainUnitPrice"
    Public Const mscMainQuantity As String = "MainQuantity"
    Public Const mscAmount As String = "Amount"
    Public Const mscTaxRate As String = "TaxRate"
    Public Const mscDiscountRate As String = "DiscountRate"
    Public Const mscDiscountAmount As String = "DiscountAmount"
    Public Const mscTaxAmount As String = "TaxAmount"
    Public Const mscItemName As String = "ItemName"
    Public Const mscItemID As String = "ItemID"
    Public Const mscStockID As String = "StockID"
    Public Const mscUnit As String = "Unit"
    Public Const mscBarcode As String = "Barcode"
    Public Const mscLotNo As String = "LotNo"
    Public Const mscExpiryDate As String = "ExpiryDate"
    Public Const mscProductionDate As String = "ProductionDate"

#End Region

#Region "Property"


#Region "Show/Hide Tool "

#End Region

#End Region

#Region "Contructor"

#End Region

#Region "Public"


#Region "InitData"

    '''' <summary>
    '''' 
    '''' </summary>
    '''' <param name="dtDetail"></param>
    '''' <param name="dtUnitConvert"></param>
    '''' <remarks></remarks>
    'Public Shared Sub SetMainValueWhenSave(ByVal dtDetail As DataTable, ByVal dtUnitConvert As DataSetDictionary.UnitConvertDataTable)
    '    If dtDetail IsNot Nothing AndAlso dtDetail.Rows.Count > 0 Then
    '        Dim dConvertRate As Decimal = 0
    '        Dim drUnitConvert As DataSetDictionary.UnitConvertRow() = Nothing
    '        For Each dr As DataRow In dtDetail.Rows
    '            If dr.RowState = DataRowState.Added OrElse dr.RowState = DataRowState.Modified Then
    '                drUnitConvert = dtUnitConvert.Select("ItemID = '" & dr(mscItemID) & "' AND UnitID = '" & dr(mscUnit) & "'")
    '                If drUnitConvert IsNot Nothing AndAlso drUnitConvert.Length > 0 Then
    '                    If drUnitConvert(0)._Operator = ConvertRateOperator.Multiply Then
    '                        dr(mscMainQuantity) = RoundDecimal(dr(mscQuantity) * drUnitConvert(0).ConvertRate)
    '                        dr(mscMainUnitPrice) = RoundDecimal(dr(mscUnitPrice) / drUnitConvert(0).ConvertRate)
    '                    Else
    '                        dr(mscMainQuantity) = RoundDecimal(dr(mscQuantity) / drUnitConvert(0).ConvertRate)
    '                        dr(mscMainUnitPrice) = RoundDecimal(dr(mscUnitPrice) * drUnitConvert(0).ConvertRate)
    '                    End If
    '                Else
    '                    dr(mscMainQuantity) = dr(mscQuantity)
    '                    dr(mscMainUnitPrice) = dr(mscUnitPrice)
    '                End If
    '            End If
    '        Next
    '    End If
    'End Sub

    'Public Shared Sub SetValueWhenCellChange(ByVal gdvDetail As DevExpress.XtraGrid.Views.Grid.GridView, ByVal iRowHandle As Integer, ByVal sFieldChange As String, ByVal drRow As DataRow, ByVal drItem As DataSetDictionary.ItemRow, ByVal oBLDic As BLDictionary, ByVal IsSalePriceIncludeTax As Boolean, ByVal isSale As Boolean)
    '    If drRow IsNot Nothing Then
    '        Dim decQuantity As Decimal = 0
    '        Dim decUnitPrice As Decimal = 0
    '        Dim decAmount As Decimal = 0
    '        Dim decDiscountRate As Decimal = 0
    '        Dim decDiscountAmount As Decimal = 0
    '        Dim decTaxRate As Decimal = 0
    '        Dim decTaxAmount As Decimal = 0

    '        If drRow.Table.Columns.Contains(mscQuantity) Then
    '            decQuantity = IIf(drRow(mscQuantity) Is DBNull.Value, 0, drRow(mscQuantity))
    '        End If

    '        If drRow.Table.Columns.Contains(mscUnitPrice) Then
    '            decUnitPrice = IIf(drRow(mscUnitPrice) Is DBNull.Value, 0, drRow(mscUnitPrice))
    '        End If

    '        If drRow.Table.Columns.Contains(mscAmount) Then
    '            decAmount = IIf(drRow(mscAmount) Is DBNull.Value, 0, drRow(mscAmount))
    '        End If

    '        If drRow.Table.Columns.Contains(mscDiscountRate) Then
    '            decDiscountRate = IIf(drRow(mscDiscountRate) Is DBNull.Value, 0, drRow(mscDiscountRate))
    '        End If


    '        If drRow.Table.Columns.Contains(mscDiscountAmount) Then
    '            decDiscountAmount = IIf(drRow(mscDiscountAmount) Is DBNull.Value, 0, drRow(mscDiscountAmount))
    '        End If


    '        If drRow.Table.Columns.Contains(mscTaxAmount) Then
    '            decTaxAmount = IIf(drRow(mscTaxAmount) Is DBNull.Value, 0, drRow(mscTaxAmount))
    '        End If

    '        If drRow.Table.Columns.Contains(mscTaxRate) Then
    '            decTaxRate = IIf(drRow(mscTaxRate) Is DBNull.Value, 0, drRow(mscTaxRate))
    '        End If

    '        Select Case sFieldChange
    '            Case mscItemID
    '                If drItem IsNot Nothing Then
    '                    SetDefaultValueWhenChangeItem(gdvDetail, iRowHandle, drRow, drItem, oBLDic, isSale)
    '                End If
    '            Case mscQuantity, mscUnitPrice
    '                If decAmount <> decQuantity * decUnitPrice Then
    '                    decAmount = decQuantity * decUnitPrice
    '                    SetCellGridValue(gdvDetail, iRowHandle, mscAmount, decAmount)
    '                    'drRow(mscAmount) = decAmount
    '                End If
    '            Case mscAmount
    '                If decDiscountAmount <> (decDiscountRate / 100) * decAmount Then
    '                    decDiscountAmount = (decDiscountRate / 100) * decAmount
    '                    SetCellGridValue(gdvDetail, iRowHandle, mscDiscountAmount, decDiscountAmount)
    '                    'drRow(mscDiscountAmount) = decDiscountAmount
    '                Else
    '                    If decTaxAmount <> (decTaxRate / 100) * (decAmount - decDiscountAmount) Then
    '                        decTaxAmount = (decTaxRate / 100) * (decAmount - decDiscountAmount)
    '                        SetCellGridValue(gdvDetail, iRowHandle, mscTaxAmount, decTaxAmount)
    '                        'drRow(mscTaxAmount) = decTaxAmount
    '                    End If
    '                End If
    '            Case mscDiscountRate
    '                If decDiscountAmount <> (decDiscountRate / 100) * decAmount Then
    '                    decDiscountAmount = (decDiscountRate / 100) * decAmount
    '                    SetCellGridValue(gdvDetail, iRowHandle, mscDiscountAmount, decDiscountAmount)
    '                    'drRow(mscDiscountAmount) = decDiscountAmount
    '                End If
    '            Case mscDiscountAmount
    '                If decTaxAmount <> (decTaxRate / 100) * (decAmount - decDiscountAmount) Then
    '                    decTaxAmount = (decTaxRate / 100) * (decAmount - decDiscountAmount)
    '                    SetCellGridValue(gdvDetail, iRowHandle, mscTaxAmount, decTaxAmount)
    '                    'drRow(mscTaxAmount) = decTaxAmount
    '                End If
    '            Case mscTaxRate
    '                If decTaxAmount <> (decTaxRate / 100) * (decAmount - decDiscountAmount) Then
    '                    decTaxAmount = (decTaxRate / 100) * (decAmount - decDiscountAmount)
    '                    SetCellGridValue(gdvDetail, iRowHandle, mscTaxAmount, decTaxAmount)
    '                    'drRow(mscTaxAmount) = decTaxAmount
    '                End If
    '            Case mscTaxAmount
    '        End Select

    '    End If
    'End Sub

    '''' <summary>
    '''' Thiet lap gia tri
    '''' </summary>
    '''' <param name="gdvDetail"></param>
    '''' <param name="colField"></param>
    '''' <param name="value"></param>
    '''' <remarks></remarks>
    'Public Shared Sub SetCellGridValue(ByVal gdvDetail As DevExpress.XtraGrid.Views.Grid.GridView, ByVal iRowHandle As Integer, ByVal colField As String, ByVal value As Object)
    '    If gdvDetail IsNot Nothing AndAlso colField IsNot Nothing Then
    '        gdvDetail.SetRowCellValue(iRowHandle, colField, value)
    '    End If
    'End Sub

    '''' <summary>
    '''' Thiet lap gia tri khi doi Item
    '''' </summary>
    '''' <param name="drRow"></param>
    '''' <param name="drItem"></param>
    '''' <remarks></remarks>
    'Public Shared Sub SetDefaultValueWhenChangeItem(ByVal gdvDetail As DevExpress.XtraGrid.Views.Grid.GridView, ByVal iRowHandle As Integer, ByVal drRow As DataRow, ByVal drItem As DataSetDictionary.ItemRow, ByVal oBLDic As BLDictionary, ByVal isSale As Boolean)
    '    If gdvDetail IsNot Nothing AndAlso drRow IsNot Nothing AndAlso drItem IsNot Nothing Then
    '        SetCellGridValue(gdvDetail, iRowHandle, mscItemName, drItem.ItemName)
    '        SetCellGridValue(gdvDetail, iRowHandle, mscStockID, drItem(mscStockID))
    '        Dim drUnitConvert As DataSetDictionary.UnitConvertRow = GetDefaultUnitByItem(drItem, oBLDic)
    '        Dim strDefaultUnit As String = drUnitConvert.UnitID
    '        SetCellGridValue(gdvDetail, iRowHandle, mscUnit, strDefaultUnit)
    '        SetCellGridValue(gdvDetail, iRowHandle, mscBarcode, drItem(mscBarcode))

    '        SetValueWhenUnitChange(gdvDetail, iRowHandle, drRow, oBLDic.TableBusinessObject, drItem, String.Empty, drUnitConvert, isSale)
    '    End If
    'End Sub


    'Public Shared Function GetDefaultUnitByItem(ByVal drItem As DataSetDictionary.ItemRow, ByVal oBLDic As BLDictionary) As DataSetDictionary.UnitConvertRow
    '    Dim sResult As DataSetDictionary.UnitConvertRow = Nothing

    '    If drItem IsNot Nothing AndAlso oBLDic IsNot Nothing AndAlso oBLDic.TableBusinessObject IsNot Nothing Then
    '        Dim arrItem As DataRow() = oBLDic.TableBusinessObject.Select("ItemID = '" & drItem.ItemID & "' AND IsDefault = 1")
    '        If arrItem Is Nothing OrElse arrItem.Length = 0 Then
    '            arrItem = oBLDic.TableBusinessObject.Select("ItemID = '" & drItem.ItemID & "' AND UnitID = '" & drItem.UnitID & "'")
    '        End If
    '        If arrItem IsNot Nothing AndAlso arrItem.Length > 0 Then
    '            sResult = arrItem(0)
    '        End If
    '    End If

    '    Return sResult
    'End Function


#End Region

#Region "Validate"
#End Region

#Region "Others"

        Public Shared Function RoundDecimal(ByVal dValue As Decimal, Optional ByVal iDecimalDigit As Integer = 2)
        Return Decimal.Round(dValue, iDecimalDigit, MidpointRounding.AwayFromZero)
    End Function

    'Public Shared Function SetValueWhenUnitChange(ByVal gdvDetail As DevExpress.XtraGrid.Views.Grid.GridView, ByVal iRowHandle As Integer, ByVal drRow As DataRow, ByVal dtUnitConvert As DataSetDictionary.UnitConvertDataTable, ByVal drItem As DataSetDictionary.ItemRow, ByVal oldUnit As String, ByVal newUnitRow As DataSetDictionary.UnitConvertRow, ByVal isSale As Boolean) As Boolean
    '    If oldUnit <> newUnitRow.UnitID AndAlso drRow.Table.Columns.Contains(mscItemID) AndAlso drRow(mscItemID) IsNot DBNull.Value Then

    '        'Dim drUnitConvert As DataSetDictionary.UnitConvertRow() = dtUnitConvert.Select("ItemID = '" & drItem.ItemID & "' AND UnitID ='" & newUnit & "'")

    '        Dim dUnitPrice As Decimal = 0

    '        If isSale Then
    '            dUnitPrice = If(drItem("SalePrice") Is DBNull.Value, 0, drItem("SalePrice"))
    '        Else
    '            dUnitPrice = If(drItem("PurchasePrice") Is DBNull.Value, 0, drItem("PurchasePrice"))
    '        End If

    '        SetCellGridValue(gdvDetail, iRowHandle, mscMainUnitPrice, dUnitPrice)

    '        If newUnitRow.IsConvertRateNull OrElse newUnitRow.ConvertRate = 0 Then
    '            dUnitPrice = 0
    '        Else
    '            If newUnitRow._Operator = ConvertRateOperator.Divide Then
    '                dUnitPrice = RoundDecimal(dUnitPrice / newUnitRow.ConvertRate)
    '            Else
    '                dUnitPrice = RoundDecimal(dUnitPrice * newUnitRow.ConvertRate)
    '            End If
    '        End If


    '        SetCellGridValue(gdvDetail, iRowHandle, mscUnitPrice, dUnitPrice)
    '    End If
    '    Return True
    'End Function

#End Region

#Region "Private Method"

#End Region

#End Region

#Region "Events"


#End Region
End Class
